import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import router from './router';
import request from './utils/request';
// import store from '../store/index.js'


Vue.config.productionTip = false
Vue.use(ElementUI) //全局引入element

Vue.prototype.$request=request //全局导入request

// //全局引入


import CustomPages from "./components/CustomPages";
import CustomDrawer from "./components/CustomDrawer";
Vue.component("CustomPages",CustomPages)
Vue.component("CustomDrawer",CustomDrawer)




//添加全局前置导航守卫
// to是到达路由的信息，from是来源的路由信息，next是帮助跳转的函数
router.beforeEach((to, from, next) => {
  // 判断token是否存在，读取token
  const token= sessionStorage.getItem('token1') 

 //token不存在，在地址输入可跳转到登录页面 && to.name!=='Register' && to.name!=='Forget'
 if(!token && to.name!=='Login' && to.name!=='Register' && to.name!=='Forget'){
  next({name: 'Login'})
 }
 else if(token && to.name==='Login'){
  next({name: 'Home'})
 }//token存在且当前在登录页面就跳转
 else{
  next()
 }
 
 //设置有token不能跳转到注册和忘记密码页面
 if (to.path === '/Register') {
   if (token) {
     next({name:'Home'})
   } else {
     next()
   }
 } else {
   next()
 }
 if (to.path === '/Forget') {
  if (token) {
    next({name:'Home'})
  } else {
    next()
  }
} else {
  next()
}

 let role=JSON.parse(sessionStorage.getItem('role'))
 let user=['/Owned','/Feedback','/Processingstatus','/Password','ModifyInformation'] //访问控制，将需要访问的页面加入
 if(role != '3' ){//用 ||无用
  //  if(role != '4'){
      if(user.includes(to.path)){
         next({name:'Auth'})
      // }else{
      //    next()
      // }
   }else{
      next()
   }
   //  next({name:'Auth'})
 }else{
    next()
 }//用户 (role != '3' || role != '4')
 
 let afteradmin=['/Allorders','/Problemview','/SellInformation'] //访问控制，将需要访问的页面加入
 if(role != '2' && afteradmin.includes(to.path)){
    next({name:'Auth'})
 }else{
    next()
 }//售后人员

 let administrators=['/Roles','/UserManagement','/Journal','/Permission'] 
 if(role != '1' && administrators.includes(to.path)){
    next({name:'Auth'})
 }else{
    next()
 }//管理员



})


new Vue({
  router,
  render: h => h(App),
}).$mount('#app')
